#!/bin/sh

[ -z "${MDEV}" ] && exit 22

DEV="${MDEV%p*}"
PART="${MDEV#nvme?n?p}"
PART="${PART#nvme?n?}"

[ -f "/sys/class/block/${DEV}/device/model" ] || exit 6
cat "/sys/class/block/${DEV}/device/model" >/dev/null 2>&1 || exit 15

MODEL=$(cat /sys/class/block/"${DEV}"/device/model)
MODEL=$(echo $MODEL) # leave unquoted, it removes spaces
MODEL="${MODEL// /_}"
SERIAL=$(cat /sys/class/block/"${DEV}"/device/serial)
SERIAL="${SERIAL// /}"
WWID=$(cat /sys/class/block/"${DEV}"/wwid)

mkdir -p /dev/disk/by-id
ln -sf "../../${MDEV}" "/dev/disk/by-id/nvme-${MODEL}_${SERIAL}${PART:+-part$PART}"
ln -sf "../../${MDEV}" "/dev/disk/by-id/nvme-${WWID}${PART:+-part$PART}"
